/*
 *
 * Copyright(c) TODO
 * licensing@realtor.org
 * http://www.realtor.org/license
 */

Ext.state.Manager.setProvider(new Ext.state.CookieProvider());

Ext.onReady(function()
{
	if(true == AD.UI.map_goog_enabled)
	{
		var gicons = [];
		gicons["S"] = new GIcon(G_DEFAULT_ICON,"http://maps.google.com/mapfiles/ms/micons/green-dot.png");
		gicons["P"] = new GIcon(G_DEFAULT_ICON,"http://maps.google.com/mapfiles/ms/micons/yellow-dot.png");
		gicons["A"] = new GIcon(G_DEFAULT_ICON,"http://maps.google.com/mapfiles/ms/micons/red-dot.png");
		gicons["U"] = new GIcon(G_DEFAULT_ICON,"http://maps.google.com/mapfiles/ms/micons/pink-dot.png");
	
		var goog_zoom_level = 10;
		var goog_geocoder;
		var goog_map;
	}

	function load_goog_map()
	{
		if(false == AD.UI.map_goog_enabled) return;
			
		 AD.UI.map_goog = new GMap2(document.getElementById("map_goog_div"));
		 AD.UI.map_goog.addControl(new GLargeMapControl());
		 AD.UI.map_goog.addControl(new GMapTypeControl());
		 AD.UI.map_goog.setCenter(new GLatLng(20,0), 2);

		 goog_geocoder = new GClientGeocoder();
		 AD.UI.map_goog.checkResize();
	 }

	function place(lat,lng, icon_index, overlay_text)
	{
		if(false == AD.UI.map_goog_enabled) return;
		
        var point = new GLatLng(lat,lng);
        AD.UI.map_goog.setCenter(point, goog_zoom_level);

        var mark = new GMarker(point, gicons[icon_index]);
        GEvent.addListener(mark, 'click', function() { mark.openInfoWindowHtml(overlay_text);});

        AD.UI.map_goog.addOverlay(mark);
        AD.UI.map_goog.checkResize();

    }

	function showAddress(address, icon_index, overlay_text)
	{
		if(false == AD.UI.map_goog_enabled) return;
		
	        goog_geocoder.getLocations(address, function (result)
	        {
	            if (result.Status.code == G_GEO_SUCCESS)
	            {
	                var p = result.Placemark[0].Point.coordinates;
	                place(p[1],p[0], icon_index, overlay_text);
	             }
	           }
	        );
	}
	
	
	
	AD.UI.fn_put_on_map_goog = function(data)
	{
		if(false == AD.UI.map_goog_enabled) return;
		
		var status = AD.UI.fn_get_status(data);
		
		var address = data.street_number + ',' + data.street_name + ' ' + data.street_suffix +' ' + data.unit_number + '<br/>' + data.city + ', ' + data.state + ' ' + data.zip;
		
		var overlay_text = AD.UI.fn_get_map_overlay_text(data, address, status);

		showAddress(address, status, overlay_text);

	};

	AD.UI.map_goog_clear = function()
	{
		if(false == AD.UI.map_goog_enabled) return;
		
		AD.UI.map_goog_win.show();
		AD.UI.map_goog.clearOverlays();
	};

	///--------------
	if(true == AD.UI.map_goog_enabled) 
	{
	
	  AD.UI.map_goog_win = new Ext.Window({
		  id:'map_goog_win',
		  width: AD.Globals.client_width * 0.30,
		  height: AD.Globals.client_height * 0.60,
		  autoScroll:true,
		  collapsible : true,
		  collapsed : true,
		  closable : false,
		  frame : true,
		  layout:'fit',		  
		  minimizable: false,
		  maximizable: true,
		  stateful :true,
		  title : 'Google Maps',
		  stateful: true,
		  stateId:'map_goog_win',
		  items : [{ xtype: 'box', autoEl: { tag: 'div',html: "<div id='map_goog_div' 	style='width: 100%; height: 600px;'>Google Maps...</div>"}}]
		  });

	  AD.UI.map_goog_win.show();
	  load_goog_map();
	}

});
